package com.shandao.shangxiu.ui;

import android.text.TextUtils;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.shandao.shangxiu.R;
import com.shandao.shangxiu.base.BaseActivity;
import com.shandao.shangxiu.util.PatternCheck;
import com.shandao.shangxiu.util.TDevice;
import com.shandao.shangxiu.util.UIHelper;


public class LoginActivity extends BaseActivity {
	private ImageView civUserHeader;// 显示用户头像
	private EditText etUserPassword;// 用户密码输入
	private EditText etPhone;// 用户账户输入
	private Button btnLogin;
	private TextView tvRegist;// 注册按钮
	private TextView tvForgetPassword;// 忘记密码按钮

	private String phone;// 用户账户
	private String password;// 用户密码
	@Override
	protected int getLayoutId() {
		return R.layout.activity_login;
	}

	@Override
	protected boolean hasToolbar() {
		return false;
	}

//	private Toolbar.OnMenuItemClickListener onMenuItemClick = new Toolbar.OnMenuItemClickListener() {
//		@Override
//		public boolean onMenuItemClick(MenuItem menuItem) {
//			String msg = "";
//			switch (menuItem.getItemId()) {
//				case R.id.action_edit:
//					msg += "Click edit";
//					break;
//				case R.id.action_share:
//					msg += "Click share";
//					break;
//				case R.id.action_settings:
//					msg += "Click setting";
//					break;
//			}
//
//			if(!msg.equals("")) {
//				Toast.makeText(LoginActivity.this, msg, Toast.LENGTH_SHORT).show();
//			}
//			return true;
//		}
//	};


	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// 為了讓 Toolbar 的 Menu 有作用，這邊的程式不可以拿掉
		getMenuInflater().inflate(R.menu.menu_main, menu);
		return true;
	}

	@Override
	public void initView() {
//		toolbar.setOnMenuItemClickListener(onMenuItemClick);
		civUserHeader = (ImageView) findViewById(R.id.civ_user_header);
		etPhone = (EditText) findViewById(R.id.et_user_phone);
		etUserPassword = (EditText) findViewById(R.id.et_userPassword);
		btnLogin = (Button) findViewById(R.id.btn_login);
		tvRegist = (TextView) findViewById(R.id.tv_regist);
		tvForgetPassword = (TextView) findViewById(R.id.tv_forget);

		// 设置监听
		btnLogin.setOnClickListener(this);
		tvRegist.setOnClickListener(this);
		tvForgetPassword.setOnClickListener(this);
	}

	@Override
	public void initData() {
//		setToolbarTitle(R.string.app_name);
	}

	@Override
	public void onClick(View view) {
		int id = view.getId();
		if (id == R.id.btn_login) {
			login();// 登录
		} else if (id == R.id.tv_regist) {
			UIHelper.showRegistActivity(this);// 注册
		} else if (id == R.id.tv_forget) {
			UIHelper.showForgetActivity(this);// 忘记密码
		}
	}

	private void login() {
		if (!isPrepareForLogin()) {
			return;
		}
//		 防止多次点击登录按钮
		btnLogin.setEnabled(false);
		if("15801818628".equals(phone) && "123456".equals(password)){
			UIHelper.showMainActivity(this);
			finish();
		}
	}

	private boolean isPrepareForLogin() {
		if (!TDevice.hasInternet()) {
			TDevice.setNetworkMethod(this);
			return false;
		}
		phone = etPhone.getText().toString().trim();
		if (!PatternCheck.isMobile(phone) && !PatternCheck.isEmail(phone)) {
			showToast(R.string.account_error);
			return false;
		}
		password = etUserPassword.getText().toString().trim();
		if (TextUtils.isEmpty(password)) {
			showToast(R.string.password_error);
			return false;
		}
		return true;
	}
}
